package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.di.scope.FragmentScope;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.entity.CoinItem;
import com.chainyoung.component_tmperson.entity.PurseAddrItem;
import com.chainyoung.component_tmperson.ui.mvp.contract.GetCoinContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.GetPurseListContract;

import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

@FragmentScope
public class GetPurseListPresenter extends BasePresenter<GetPurseListContract.Model, GetPurseListContract.View> {

    @Inject
    public GetPurseListPresenter(GetPurseListContract.Model model, GetPurseListContract.View rootView) {
        super(model, rootView);
    }


    public void getPurseAddrList(TreeMap<String, Object> map) {
        addSubscribe(mModel.getPurseList(map)
                .compose(RxUtils.<CommonList<PurseAddrItem>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<CommonList<PurseAddrItem>>(mView) {
                    @Override
                    public void onNext(CommonList<PurseAddrItem> data) {
                        mView.onGetPurseListSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetPurseListSuccess(null);
                                return;
                            }
                        }
                        mView.onGetPurseListFailed();
                        super.onError(e);
                    }
                }));
    }


    public void delPurseAddr(TreeMap<String, Object> map) {
        addSubscribe(mModel.delPurseAddr(map)
                .compose(RxUtils.<String>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<String>(mView,true) {
                    @Override
                    public void onNext(String data) {
                        mView.onDelPurseAddrSuccessed();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


}


